//
//  ExchangeRateController.m
//  CenturyCity
//
//  Created by beisheng03 on 2017/12/22.
//  Copyright © 2017年 beisheng. All rights reserved.
//

#import "ExchangeRateController.h"
#import "ExchangeRateCell.h"
#import "ExchangeRateHeaderView.h"
#import "ExchangeRateCulVC.h"
#import "ExchangRateModel.h"

#import "HuiLVHeaderView.h"
@interface ExchangeRateController ()

@property (nonatomic, strong) ExchangeRateHeaderView *headerView;


@property (nonatomic, strong) NSMutableArray<NSArray *> *models;


/** <#note#> */
@property (nonatomic, weak) UILabel *bottmLabel;
@end

@implementation ExchangeRateController




- (UILabel *)bottmLabel{
    
    if (!_bottmLabel) {
        
        UILabel *label = [[UILabel alloc]init];
        label.font = MYFONT(13);
        label.textColor = themeColor;
        label.text = @"声明: 菲度参考当地主要银行BDO/BPI汇率";
        NSMutableDictionary *tempDic = [[NSMutableDictionary alloc]init];
        tempDic[NSFontAttributeName] = MYFONT(13);
        CGSize size = [label.text boundingRectWithSize:CGSizeMake(KScreenW - 40, 0) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:tempDic context:nil].size;
        
        CGFloat tempMargin = IPhoneX ? 44 : 10;
        label.frame = CGRectMake(20, KScreenH - size.height - tempMargin, KScreenW - 40, size.height);
        _bottmLabel = label;
        [self.view addSubview:_bottmLabel];
        
    }
    
    
    
    return _bottmLabel;
}


- (ExchangeRateHeaderView *)headerView{
    
    if (!_headerView) {
        
        ExchangeRateHeaderView *temp = (ExchangeRateHeaderView *)[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([ExchangeRateHeaderView class]) owner:nil options:nil].firstObject;
        CGFloat viewY = IPhoneX ? 88 : 64;
        temp.frame = CGRectMake(0, viewY, KScreenW, 64);
        _headerView = temp;
        [self.view addSubview:temp];
    }
    return _headerView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"汇率查询";
    
    self.view.backgroundColor = viewBgGrey;
    
    self.tag = @"now";
    
    self.plainTableView.backgroundColor = viewBgGrey;

    [self.plainTableView registerNib:[UINib nibWithNibName:NSStringFromClass([ExchangeRateCell class]) bundle:nil] forCellReuseIdentifier:[ExchangeRateCell identifierStr]];
    
    [self.plainTableView registerNib:[UINib nibWithNibName:NSStringFromClass([HuiLVHeaderView class]) bundle:nil] forHeaderFooterViewReuseIdentifier:[HuiLVHeaderView identifierStr]];
    
    
    self.plainTableView.contentInset = UIEdgeInsetsMake(64, 0, 0,0 );
        
    MJWeakSelf
    self.plainTableView.mj_header = [XMGRefreshHeader headerWithRefreshingBlock:^{
        
        [weakSelf loadData];
        
    }];
    
    [weakSelf.plainTableView.mj_header beginRefreshing];
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"汇率计算" style:UIBarButtonItemStylePlain target:self action:@selector(exchangeRatecalCulation)];
    
    
    
    self.headerView.backgroundColor = viewBgGrey;
    
    self.bottmLabel.backgroundColor = viewBgGrey;
    
}

- (void)exchangeRatecalCulation{
    
    ExchangeRateCulVC *temp = [[ExchangeRateCulVC alloc]init];
    
    
    [self.navigationController pushViewController:temp animated:YES];
}


/**
 *汇率接口
 *  默认显示最新
 *  tag   old  请求次新的汇率
 * */


- (void)loadData{
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"tag"] = self.tag;
    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools] sendRequestWithHttpMethod:GET_METHOD URLPath:@"Rate" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
//            MYJSONSTR
            
//            NSMutableDictionary *dic = responseObject[@"data"];
//
//            NSMutableArray *array = [NSMutableArray array];
//            [dic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
//
//                if ([key isEqualToString:@"id"] || [key isEqualToString:@"date"] || [key isEqualToString:@"type"]) {
//
//                } else {
//
//                    ExchangRateModel *model = [[ExchangRateModel alloc]init];
//
//                    model.name = key;
//
//                    model.exchangeRate = obj;
//
//                    [array addObject:model];
//                }
//
//            }];
            
            
            self.models = responseObject[@"data"][@"rate"];
            
            
            CGFloat lastH = 0;
            for (NSArray *temp in self.models) {
                NSString *str = temp[1];
                CGFloat labelH = [str sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:MYFONT(16), NSFontAttributeName, nil]].width;
                
                if (labelH > lastH) {
                    lastH = labelH;
                }
            }
            
            myViewModel.huiLvLabelH = lastH;
            
   
            [weakSelf.plainTableView.mj_header endRefreshing];
            
            [weakSelf.plainTableView reloadData];
            
            
        }else{
            
            [weakSelf.plainTableView.mj_header endRefreshing];
            
            [SVProgressHUD showInfoWithStatus:error.domain];
        }
        
    }];
    
    
}




#pragma mark - tableviewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if (self.models == nil) {
        return 0;
    } else {
        return 1;
    }
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.models == nil) {
        return 0;
    } else {
        return self.models.count;
    }}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ExchangeRateCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[ExchangeRateCell identifierStr]];
    
    tableCell.model = self.models[indexPath.row];
    
    
    return tableCell;
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 60;
}



- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    
    return 0.00001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return 36;
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    
    return [[UIView alloc]init];
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    HuiLVHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:[HuiLVHeaderView identifierStr]];
    
    header.labelH = myViewModel.huiLvLabelH;
    
    return  header;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
